package com.macllvm.whale.ui;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.util.Log;
import java.util.Observer;
import java.util.Observable;
import com.macllvm.whale.Event;
import com.macllvm.whale.model.*;
import com.macllvm.whale.entity.*;
import com.macllvm.whale.adapter.*;
import com.macllvm.whale.protocol.*;
import com.macllvm.whale.R;



public class TransfersFragment extends Fragment  {

    private static final int SHOW_TRANSFER_IN = 0;
    private static final int SHOW_TRANSFER_OUT = 1;
    private ListView mTransfersInListView;
    private ListView mTransfersOutListView;
    private Context mContext;
    private TransferAdapter mTransferInAdapter;
    private TransferAdapter mTransferOutAdapter;
    private RadioButton segmentTransferIn;
    private RadioButton segmentTransferOut;
    private RadioGroup  mSegmentBar;
    private ProtocolController mController;
    private int mShowState = SHOW_TRANSFER_IN;
    private View mView;
    private long lastUpdatedTime = 0;
    private TransferInObserver mTransferInObserver = new TransferInObserver();
    private TransferOutObserver mTransferOutObserver = new TransferOutObserver();


    @Override
    public void onAttach (Activity activity) {
        super.onAttach(activity);
        mContext = (Context)activity;
        mTransferInAdapter = new TransferAdapter(mContext);
        mTransferOutAdapter = new  TransferAdapter(mContext);
        mController = ProtocolController.getInstance();
        mTransferInAdapter.setDataSource(mController.getTransferInManager().getDataSource());
        mTransferOutAdapter.setDataSource(mController.getTransferOutManager().getDataSource());
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(null == mView) {
            mView = inflater.inflate(R.layout.transfers_fragment, container, false);
            mTransfersInListView = (ListView) mView.findViewById(R.id.transfers_in_list);
            mTransfersOutListView = (ListView) mView.findViewById(R.id.transfers_out_list);
            mSegmentBar = (RadioGroup) mView.findViewById(R.id.segment_group);
            mSegmentBar.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioButtonId = group.getCheckedRadioButtonId();
                switch (checkedId) {
                    case R.id.segment_in:
                         if (mShowState != SHOW_TRANSFER_IN) {
                             mTransfersInListView.setVisibility(View.VISIBLE);
                             mTransfersOutListView.setVisibility(View.GONE);
                             mShowState = SHOW_TRANSFER_IN;
                         }
                         break;
                    case R.id.segment_out:
                         if (mShowState != SHOW_TRANSFER_OUT) {
                             mTransfersInListView.setVisibility(View.GONE);
                             mTransfersOutListView.setVisibility(View.VISIBLE);
                             mShowState = SHOW_TRANSFER_OUT;
                         }
                         break;
                }

                }
            });
        }else{
            ViewGroup parent = (ViewGroup)mView.getParent();
            if (null != parent) {
                parent.removeView(mView);
            }
        }
        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mTransfersInListView.setAdapter(mTransferInAdapter);
        mTransfersOutListView.setAdapter(mTransferOutAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();

        if(mShowState == SHOW_TRANSFER_IN)
            handler.sendEmptyMessage(Event.EVENT_REFRESH_TRANSFER_IN);
        else
            handler.sendEmptyMessage(Event.EVENT_REFRESH_TRANSFER_OUT);
    }

    @Override
    public void onStart() {
        super.onStart();
        mController.getTransferInManager().addObserver(mTransferInObserver);
        mController.getTransferOutManager().addObserver(mTransferOutObserver);
    }

    @Override
    public void onStop() {
        super.onStop();
        mController.getTransferInManager().deleteObserver(mTransferInObserver);
        mController.getTransferOutManager().deleteObserver(mTransferOutObserver);
    }


    private class TransferInObserver implements Observer {
        @Override
        public void update(Observable object, Object argument) {
            if (mShowState == SHOW_TRANSFER_IN)
                handler.sendEmptyMessage(Event.EVENT_REFRESH_TRANSFER_IN);
        }
    }

    private class TransferOutObserver implements Observer {
        @Override
        public void update(Observable object, Object argument) {
            if (mShowState == SHOW_TRANSFER_OUT)
                 handler.sendEmptyMessage(Event.EVENT_REFRESH_TRANSFER_OUT);

        }
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
        switch (msg.what) {
            case Event.EVENT_REFRESH_TRANSFER_IN:
                mTransferInAdapter.notifyDataSetChanged();
                break;
            case Event.EVENT_REFRESH_TRANSFER_OUT:
                mTransferOutAdapter.notifyDataSetChanged();
                break;
        }
        }
    };

}
